//
//  LXSignContractApplyViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2022/1/4.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXSignContractDetailViewController.h"
#import "LXSignDetailTableViewCell.h"
#import "LXSignContractModel.h"
#import "LXSignShowContractTableViewCell.h"
#import "LXBigImageView.h"
#import "LXRescindViewController.h"
#import "LXSignContractApplyViewController.h"
#import "LXRemarkTabelViewCell.h"
#import "XIeYiPageVC.h"
#import "LXSignPayRentPlanTableViewCell.h"
#import "LXReturnCarViewController.h"
@interface LXSignContractDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) UIScrollView *scrollView;

@property (nonatomic) NSMutableArray *contractData;
@property (nonatomic) NSString *checkUrl,*driverUrl,*carUrl;
@property (nonatomic) LXSignContractModel *model;
@property (nonatomic) NSString *remark;
@property (nonatomic) UIView *bottomView;
@property (nonatomic) BOOL isRemarkEdit;
@end

@implementation LXSignContractDetailViewController

- (void)pop{
    if (self.isRemarkEdit) {
        [Tool showAlertTitle:@"确定退出吗?" msg:@"退出将会导致备注不能保存" cancelTitle:@"取消" commitBtnTitle:@"退出" cancelHandlerBlock:^{
        } commitHandlerBlock:^{
            [super pop];
        } onVC:self];
    }
    else {
        [super pop];
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self layoutCustomNav:self.view title:@"签约详情"];
    
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXSignDetailTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXSignDetailTableViewCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXSignShowContractTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXSignShowContractTableViewCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXRemarkTabelViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXSignPayRentPlanTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXSignPayRentPlanTableViewCell class])];

    
    [self loadData];
    
   

}

- (void)loadData{
    
    [MSNetworkManager getRevertUrl:[DOMAIN_URL stringByAppendingString:SigncontractDetail_URL] params:@{@"id":self.signContractModel.signContractId} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            
          
            self.contractData = [NSMutableArray array];
            
            LXSignContractModel *model = [LXSignContractModel mj_objectWithKeyValues:resonseObj];
            
            self.remark = model.remark;
            
            // 如果是通过一键签约(已经预约过的)
            if (!StringIsEmpty(model.appointmentId)) {
                
            }
            else {
                model.carBrand = model.signCarBrand;
                model.carModel = model.signModel;
            }
            self.model = model;
            
            [self setBottomView];
            
            NSDictionary *dic0 = @{
                    @"sectionTitle":@"",
                    @"sectionHeight":@(0.0001),
                    @"list":@[@{
                        @"title":@"客户信息",
                        @"key":@"customerName",
                        @"value":[NSString stringWithFormat:@"%@ %@",model.driverName,model.driverPhone],
                        @"height":@(kComHeight)
                    }]
            };
            [self.data addObject:dic0];
            
            if (!StringIsEmpty(model.appointmentId)) {
                
                NSDictionary *dic1 =
                    @{
                        @"sectionTitle":@"预约记录",
                        @"sectionKey":@"appointmentKey",
                        @"sectionHeight":@(kComHeight),
                        @"list":@[
                            
                            @{
                                @"title":@"咨询车型",
                                @"key":@"carBrand",
                                @"value":[NSString stringWithFormat:@"%@ %@",model.carBrand,model.carMode],
                                @"height":@(kComHeight)
                            },
                            
                            @{
                                @"title":@"预约地址",
                                @"key":@"address",
                                @"value":[NSString stringWithNull:model.companyAddr],
                                @"height":@(kComHeight)
                            },
                            @{
                                @"title":@"预约时间",
                                @"key":@"appointmentTime",
                                @"value":[NSString stringWithNull:model.appointTimeStr],
                                @"height":@(kComHeight)
                            },
                        ]
                    };
                
                [self.data addObject:dic1];
            }
            
            NSDictionary *dic2 =
            @{
                @"sectionTitle":@"当前签约详情",
                @"sectionKey":@"signKey",
                @"sectionHeight":@(kComHeight),
                @"list":@[
                    
                    @{
                        @"title":@"签约类型",
                        @"key":@"signType",
                        @"detailTextColor":mainColor,
                        @"value":model.signType==1?@"租车":(model.signType==2?@"购车":@"以租代购"),
                        @"height":@(kComHeight)
                    },
                    
                    @{
                        @"title":@"车牌号",
                        @"key":@"carNum",
                        @"value":[NSString stringWithNull:model.carNum],
                        @"height":@(kComHeight)
                    },
                    @{
                        @"title":@"品牌型号",
                        @"key":@"carBrand",
                        @"value":[NSString stringWithFormat:@"%@ %@",model.signCarBrand,model.signModel],
                        @"height":@(kComHeight)
                    },
                    @{
                        @"title":@"签约企业",
                        @"key":@"companyName",
                        @"value":[NSString stringWithNull:model.companyName],
                        @"height":@(kComHeight)
                    },
                    @{
                        @"title":@"合约时间",
                        @"key":@"signTime",
                        @"value":[NSString stringWithFormat:@"%@ 至 %@",model.beginTime,model.endTime],
                        @"height":@(kComHeight)
                    },
                    @{
                        @"title":model.signType==1?@"每期金额":@"月供金额",
                        @"key":@"rent",
                        @"value":model.signType==1?[NSString stringWithFormat:@"%@元(总共分%@期)",model.rent,model.signPeriods]:[NSString stringWithFormat:@"%@元(总共分%@期)",model.rent,model.signPeriods],
                        @"height":@(kComHeight)
                    },
                    @{
                        @"title":model.signType==1?@"押金":@"首付金额",
                        @"key":@"rent",
                        @"value":[NSString stringWithFormat:@"%@元",model.depost],
                        @"height":@(kComHeight)
                    },

                    @{
                        @"title":@"交租日期",
                        @"key":@"payDate",
                        @"value":[NSString stringWithFormat:@"每月%@日",[NSString stringWithNull:model.payDate]],
                        @"height":@(kComHeight)
                    },
                    @{
                        @"title":@"企业应付佣金",
                        @"key":@"platformBrokerage",
                        @"value":[NSString stringWithFormat:@"%@元",model.platformBrokerage],
                        @"height":self.signContractModel.fromType==2?@(kComHeight):@(0)
                    },
                    @{
                        @"title":@"合同",
                        @"key":@"contract",
                        @"value":model.images,
                        @"height":@(370)
                    },
                ]
            };
            
            [self.data addObject:dic2];
            
            
            
            NSDictionary *dic3 = @{
                @"sectionTitle":@"",
                @"sectionHeight":@(0.0001),
                @"list":@[
                    @{
                        @"title":@"备注",
                        @"key":@"remarks",
                        @"height":@(150)
                    }
                ]
            };
            [self.data addObject:dic3];
            
            NSDictionary *dic4 = @{
                @"sectionTitle":@"",
                @"sectionHeight":@(0.0001),
                @"list":@[
                    @{
                        @"title":@"租金支付计划表",
                        @"key":@"planForm",
                        @"height":@([LXSignPayRentPlanTableViewCell getHeight:model.intoFLows]),
                        @"value":model.intoFLows,
                    }
                ]
            };
            [self.data addObject:dic4];

//            //图片类别: : 1:合同 | 2:验收  3:车辆  4:驾驶证  5：行驶证
            NSMutableArray *contractArys = [NSMutableArray array];
            NSDictionary *checkDic,*driverDic,*carDic;
            for (NSDictionary *dic in model.images) {
                if ([dic[@"type"] intValue] == 1) {
                    [contractArys addObject:dic];
                }
                else if([dic[@"type"] intValue] == 2){
                    checkDic = dic;
                }
                else if([dic[@"type"] intValue] == 3){
                    carDic = dic;
                }
                else if([dic[@"type"] intValue] == 5){
                    driverDic = dic;
                }
            }
              // 合同
            for (NSDictionary *dic in contractArys) {
                [self.contractData addObject:[NSString stringWithFormat:@"%@%@",HTTP_URL,dic[@"path"]]];
            }
            // 验车单
            if (!ObjectIsEmpty(checkDic)) {
                self.checkUrl = [NSString stringWithFormat:@"%@%@",HTTP_URL,checkDic[@"path"]];
            }
            // 行驶证
            if (!ObjectIsEmpty(driverDic)) {
                self.driverUrl = [NSString stringWithFormat:@"%@%@",HTTP_URL,driverDic[@"path"]];
            }
            // 车辆照片
            if (!ObjectIsEmpty(carDic)) {
                self.carUrl = [NSString stringWithFormat:@"%@%@",HTTP_URL,carDic[@"path"]];
            }

         
            [self.tableView reloadData];
        }
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
       
    } withShowHud:YES];
    
    
    
  
    
    
   
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.data.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dic = self.data[section];
    return [dic[@"list"] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *section = self.data[indexPath.section];
    NSDictionary *dic = section[@"list"][indexPath.row];
    return [dic[@"height"] doubleValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *section = self.data[indexPath.section];
    NSDictionary *dic = section[@"list"][indexPath.row];
    
    
    // 租金计划表
    if([dic[@"key"]  isEqualToString:@"planForm"]){
        LXSignPayRentPlanTableViewCell *cell = [[LXSignPayRentPlanTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([LXSignPayRentPlanTableViewCell class]) WithData:dic[@"value"] WithSignType:self.model.signType IsShow:YES];
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
       [cell setClipsToBounds:YES];
       cell.titleLab.text = dic[@"title"];
       return cell;
   }
    
    
    
    // 备注
    if([dic[@"key"]  isEqualToString:@"remarks"]){
       LXRemarkTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
        cell.saveBtn.hidden = NO;
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
       [cell setClipsToBounds:YES];
       cell.textView.placeholder = @"请输入 ~";
       cell.textView.maxLength = 200;
       cell.remarkTitle.text = @"备注";
       cell.textView.text = self.remark;
       [cell setTextViewEdit:^(NSString * _Nonnull textStr) {
           self.remark  = textStr;
           self.isRemarkEdit = YES;
       }];
        
        [cell.saveBtn whenTapped:^{
            if (StringIsEmpty(self.remark)) {
//                showAlert(@"请输入备注");
//                return;
                self.remark = @"";
            }
            [self.view endEditing:YES];
            [self updateRemark];
            
        }];
        
       return cell;
   }
    
    if ([dic[@"key"] isEqualToString:@"contract"]) {
        
        LXSignShowContractTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXSignShowContractTableViewCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setClipsToBounds:YES];
       
        cell.contractData = self.contractData;
        [cell.checkImageV sd_setImageWithURL:[NSURL URLWithString:self.checkUrl] placeholderImage:Image(@"未上传")];
        
        [cell.driverImageV sd_setImageWithURL:[NSURL URLWithString:self.driverUrl] placeholderImage:Image(@"未上传")];

        if (!StringIsEmpty(self.driverUrl)) {
            cell.contractTipsLab.hidden = NO;
        }
        else {
            cell.contractTipsLab.hidden = YES;
        }
        [cell.carImageV sd_setImageWithURL:[NSURL URLWithString:self.carUrl] placeholderImage:Image(@"未上传")];
    
        [cell.contractImageV whenTapped:^{
            LXBigImageView *view = [[LXBigImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) WithShowImageAry:self.contractData];
            [[UIApplication sharedApplication].delegate.window addSubview:view];
        }];
        [cell.checkImageV whenTapped:^{
            if (!StringIsEmpty(self.checkUrl)) {
                [LXSignShowContractTableViewCell showBigImage:cell.checkImageV.image];
            }
        }];
        [cell.carImageV whenTapped:^{
            if (!ObjectIsEmpty(self.carUrl)) {
                [LXSignShowContractTableViewCell showBigImage:cell.carImageV.image];
            }
        }];

        return cell;
    }
    
    
   
    
    LXSignDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXSignDetailTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLab.text = dic[@"title"];
    cell.valueLab.text = dic[@"value"];
    
    if ([dic[@"key"] isEqualToString:@"signType"]) {
        if (self.model.signType == 2) {
            cell.valueLab.textColor = ColorWith3C6CFC;
        }
        else {
            cell.valueLab.textColor = [UIColor colorWithHexString:@"#FF921C"];
        }
    }
    else {
        cell.valueLab.textColor = ColorWith333333;
    }
    
    if ([dic[@"key"] isEqualToString:@"customerName"]) {
        cell.callPhoneImagV.hidden = NO;
    }
    else {
        cell.callPhoneImagV.hidden = YES;
    }
    
    if ([dic[@"key"] isEqualToString:@"signTime"]) {
        [cell.valueLab addAttributes:TextAttrCorlor(ColorWithAAAAAA) forString:@"至"];
    }
    else {
    }
    
    return cell;
}
- (void)updateRemark{
    NSString *url = [NSString stringWithFormat:@"%@",SigncontractUpdate_URL];
    
    NSDictionary *dic = @{
        @"signContractId":self.signContractModel.signContractId,//合约id
        @"remark":self.remark
    };
    
    [MSNetworkManager postRevertUrl:url params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            kUserDefaultSetBool(YES, kRefreshStr);
            showAlert(@"保存成功");
            self.isRemarkEdit = NO;
        }
       
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    NSDictionary *dic = self.data[section];
    UIView *vv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, [dic[@"sectionHeight"] doubleValue])];
    vv.backgroundColor = [UIColor colorWithHexString:@"#FAFBFC"];
    [vv setClipsToBounds:YES];
    
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 10, kScreenWidth, 40)];
    backView.backgroundColor = [UIColor whiteColor];
    [vv setClipsToBounds:YES];
    [vv addSubview:backView];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15, 17,3, 16)];
    line.backgroundColor = [UIColor colorWithHexString:@"#FF921C"];
    [backView addSubview:line];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(line.X_width + 10, 0, 100, vv.height)];
    lab.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
    lab.textColor = UIColor.blackColor;
    lab.text = dic[@"sectionTitle"];
    [backView addSubview:lab];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth - 120, 15, 100, 20)];
    btn.titleLabel.font = [UIFont systemFontOfSize:12];
    [backView addSubview:btn];
    
    NSInteger day = [NSString dateCompare:[self.model.sysTime substringToIndex:10] secondTime:self.model.endTime];

    
    // 预约记录
    if ([dic[@"sectionKey"] isEqualToString:@"appointmentKey"]) {
        [btn setImage:Image(@"right_row_icon") forState:UIControlStateNormal];
        [btn setTitle:@"查看更多" forState:UIControlStateNormal];
        [btn setTitleColor:ColorWithAAAAAA forState:UIControlStateNormal];
        [btn setIconInRightWithSpacing:3];
    }
    else if([dic[@"sectionKey"] isEqualToString:@"signKey"]){
        
        // 解约处理
        if(self.signContractModel.signStatus==3){
            
            
        }
        else if(self.model.reNewFlag == 1 && day <= 0){
            [btn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
            if (day == 0) {
                [btn setTitle:@"合约今天到期" forState:UIControlStateNormal];
            }
            else if(day < 0){
                [btn setTitle:[NSString stringWithFormat:@"合约即将%@天后到期",@(labs(day))] forState:UIControlStateNormal];
            }
            else {
                [btn setTitle:@"合约已到期" forState:UIControlStateNormal];
            }
            btn.width = [NSString getLabelOptimumWidth:btn.titleLabel WithFont:12] + 3;
            [btn setBackgroundColor:[[UIColor colorWithHexString:@"#FF921C"] colorWithAlphaComponent:0.1]];
            [btn setTitleColor:[UIColor colorWithHexString:@"#FF921C"] forState:UIControlStateNormal];
        }
    
    }
    
    
    return vv;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    NSDictionary *dic = self.data[section];
    return [dic[@"sectionHeight"] doubleValue];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *section = self.data[indexPath.section];
    NSDictionary *dic = section[@"list"][indexPath.row];
    if ([dic[@"key"] isEqualToString:@"customerName"]) {
        NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"telprompt://%@",self.model.driverPhone];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:nil];
    }
 
}

- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc]init];
    }
    return _data;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kViewTopHeight, ScreenWidth, kScreenHeight - kViewTopHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#F7F8FA"];
//        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = kComHeight;
        _tableView.showsVerticalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        
    }
    return _tableView;
}
#pragma mark ---- 确认新增/修改点击事件
-(void)setBottomView{

    
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = UIColor.whiteColor;
    self.bottomView = bottomView;
    
    NSInteger date = [NSString dateCompare:[self.model.sysTime substringToIndex:10] secondTime:self.model.endTime];
    // 业务员
    if(self.model.fromType == 2 && (self.model.companyComfirmStat == 0 || self.model.driverComfirmStat == 0)){
        
       
        
        if (self.model.companyComfirmStat == 0) {
            
            bottomView.frame = CGRectMake(0, kScreenHeight - 80, kScreenWidth, 80);
            
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(15, 5, 140, 17)];
            [btn setImage:[UIImage imageNamed:@"small_unSelect_icon"] forState:UIControlStateNormal];
            [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
            [btn setImage:[UIImage imageNamed:@"small_select_icon"] forState:UIControlStateSelected];
            [btn setTitle:@" 我已阅读并同意遵守" forState:UIControlStateNormal];
            [btn setTitleColor:ColorWithAAAAAA forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:12];
            [btn whenTapped:^{
                btn.selected = !btn.isSelected;
            }];
            [bottomView addSubview:btn];
            
            UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(btn.X_width, 5, 100, 15)];
            [btn1 setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
            [btn1 setTitle:@"《三方协议》" forState:UIControlStateNormal];
            [btn1 setTitleColor:mainColor forState:UIControlStateNormal];
            btn1.titleLabel.font = [UIFont systemFontOfSize:12];
            [bottomView addSubview:btn1];
            [btn1 whenTapped:^{
                XIeYiPageVC *vc = [[XIeYiPageVC alloc] init];
                vc.titleString = @"三方协议";
                vc.jf = self.model.companyName;
                vc.yf = self.model.driverName;
                vc.type = @"3";
                [self.navigationController pushViewController:vc animated:YES];
            }];
            
            UIButton *queRenBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, btn.Y_height + 5, ScreenWidth - 30, kComHeight)];
            queRenBtn.backgroundColor = mainColor;
            [queRenBtn addCorner:4];
            queRenBtn.tag = 1;
            [queRenBtn setTitle:@"确认" forState:UIControlStateNormal];
            [queRenBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
            queRenBtn.titleLabel.font = Fond16;
            [bottomView addSubview:queRenBtn];
            self.tableView.height = kScreenHeight - kViewTopHeight - 85;
            [queRenBtn whenTapped:^{
        
                if (btn.isSelected==NO) {
                    showAlert(@"请先阅读并勾选三方协议");
                    return;
                }
                
                NSString *url = [NSString stringWithFormat:@"%@",SigncontractUpdate_URL];
                NSDictionary *dic = @{
                    @"signContractId":self.signContractModel.signContractId,//合约id
                    @"companyComfirmStat":@(1)
                };
                
                [MSNetworkManager postRevertUrl:url params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
                    if (isSuccess) {
                        kUserDefaultSetBool(YES, kRefreshStr);
                        showAlert(@"确认成功");
                        self.model.companyComfirmStat = 1;
                        [self.bottomView removeAllSubviews];
                        [self setBottomView];
                    }
                } failure:^(NSUInteger code, NSString * _Nonnull message) {
                } withShowHud:YES];
            }];
        }
       else if (self.model.driverComfirmStat == 0) {
            
           bottomView.frame = CGRectMake(0, kScreenHeight - 60, kScreenWidth, 60);
            
            UIButton *queRenBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, 15, ScreenWidth - 30, kComHeight)];
            [queRenBtn addCorner:4];
            queRenBtn.tag = 1;
            [queRenBtn setTitle:@"待司机确认" forState:UIControlStateNormal];
            [queRenBtn setTitleColor:ColorWithAAAAAA forState:UIControlStateNormal];
            queRenBtn.titleLabel.font = Fond16;
           queRenBtn.backgroundColor = ColorWithF5F5F5;
            [bottomView addSubview:queRenBtn];
            self.tableView.height = kScreenHeight - kViewTopHeight - 75;
            
        }
        [self.view addSubview:bottomView];
    }
    else if (self.model.reNewFlag==1 && self.model.signStatus != 3) {
//    else if (self.model.signStatus != 3) {
        [self.view addSubview:bottomView];
        bottomView.frame = CGRectMake(0, kScreenHeight - 65, kScreenWidth, 65);
        
        NSInteger day =  [NSString compareDate:[self.model.sysTime substringToIndex:10] withDate:self.model.endTime withFormat:@"yyyy-MM-dd"];
        
        if (day >= 0) {
            UIButton *jieYueBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, (ScreenWidth - 40) / 2, kComHeight)];
            jieYueBtn.backgroundColor = UIColor.whiteColor;
            [jieYueBtn addCorner:4 AndLine:mainColor];
            [jieYueBtn setTitle:@"解约退车" forState:UIControlStateNormal];
            [jieYueBtn setTitleColor:mainColor forState:UIControlStateNormal];
            jieYueBtn.titleLabel.font = Fond16;
            [bottomView addSubview:jieYueBtn];
            jieYueBtn.tag = 1;
            [jieYueBtn addTarget:self action:@selector(clickBottomView:) forControlEvents:UIControlEventTouchUpInside];
            
            
            UIButton *qianBtn = [[UIButton alloc] initWithFrame:CGRectMake(jieYueBtn.X_width + 10, 10, (ScreenWidth - 30) / 2, kComHeight)];
            qianBtn.backgroundColor = mainColor;
            [qianBtn addCorner:4];
            qianBtn.tag = 2;
            [qianBtn setTitle:@"一键续约" forState:UIControlStateNormal];
            [qianBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
            qianBtn.titleLabel.font = Fond16;
            [bottomView addSubview:qianBtn];
            [qianBtn addTarget:self action:@selector(clickBottomView:) forControlEvents:UIControlEventTouchUpInside];
            
            
            
        }
        else if (day < 0) {
            [self.view addSubview:bottomView];
            UIButton *jieYueBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, ScreenWidth - 30, kComHeight)];
            jieYueBtn.backgroundColor = mainColor;
            [jieYueBtn addCorner:4];
            jieYueBtn.tag = 1;
            [jieYueBtn setTitle:@"解约退车" forState:UIControlStateNormal];
            [jieYueBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
            jieYueBtn.titleLabel.font = Fond16;
            [bottomView addSubview:jieYueBtn];
            [jieYueBtn addTarget:self action:@selector(clickBottomView:) forControlEvents:UIControlEventTouchUpInside];
            
        }
        self.tableView.height = kScreenHeight - kViewTopHeight - 75;
    }
    else if(self.model.signStatus != 3){
        [self.view addSubview:bottomView];
        
        bottomView.frame = CGRectMake(0, kScreenHeight - 65, kScreenWidth, 65);

        
        UIButton *jieYueBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, ScreenWidth - 30, kComHeight)];
        jieYueBtn.backgroundColor = mainColor;
        [jieYueBtn addCorner:4];
        jieYueBtn.tag = 1;
        [jieYueBtn setTitle:@"解约退车" forState:UIControlStateNormal];
        [jieYueBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        jieYueBtn.titleLabel.font = Fond16;
        [bottomView addSubview:jieYueBtn];
        [jieYueBtn addTarget:self action:@selector(clickBottomView:) forControlEvents:UIControlEventTouchUpInside];
        self.tableView.height = kScreenHeight - kViewTopHeight - 75;
    }
    
   
}



- (void)clickBottomView:(UIButton *)sender{
  
    if (sender.tag == 1) {
//        LXRescindViewController *vc = [[LXRescindViewController alloc] init];
//        vc.signContractId = self.model.signContractId;
//        vc.fromVC = @"LXSignRecordViewController";
//        [self pushToNextController:vc];
        
       
        [MSNetworkManager getRevertUrl:GET_GetReturnCarDetail params:@{@"carId":self.model.carId} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
            if (isSuccess) {
                CheLiangModel *model = [CheLiangModel mj_objectWithKeyValues:resonseObj];

                LXReturnCarViewController *vc = [[LXReturnCarViewController alloc] init];
                vc.carModel = model;
                vc.fromVc = self.fromVc;
                [self pushToNextController:vc];


            }
        } failure:^(NSUInteger code, NSString * _Nonnull message) {
        } withShowHud:YES];
        
    }
    else {
        [self getImagesData];
    }
    
    
}

-(void)getImagesData{
    
    
    LXSignContractApplyViewController *vc = [[LXSignContractApplyViewController alloc] init];
    vc.signContractModel = self.model;
    vc.type = 3;

    
    NSDictionary *dic = @{
        @"businessType":@"1",
        @"types":[NSString toJSONData:@[@"2",@"3",@"5"]],
        @"businessId":self.model.signContractId,
    };
    [MSNetworkManager postRevertUrl:GET_GetByBusinessId params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            
           
            NSDictionary *checkDic,*carDic,*driverDic;
            for (NSDictionary *dic in resonseObj) {
                
                if([dic[@"type"] intValue] == 2){
                    checkDic = dic;
                }
              
                else if([dic[@"type"] intValue] == 3){
                    carDic = dic;
                }
                else if([dic[@"type"] intValue] == 5){
                    driverDic = dic;
                }
            }
            // 验车单
            if (!ObjectIsEmpty(checkDic)) {
                vc.checkCarUrl = checkDic[@"path"];
            }
            // 车辆照片
            if (!ObjectIsEmpty(carDic)) {
                vc.carUrl = carDic[@"path"];
            }
            // 行驶证
            if (!ObjectIsEmpty(driverDic)) {
                vc.driverUrl = driverDic[@"path"];
            }
        }
        [self pushToNextController:vc];
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self pushToNextController:vc];
    } withShowHud:YES];
    
    
    
}




@end
